package com.qjdchina.sku.boss.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.qjdchina.sku.model.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.commons.model.PageModel;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.BrandManager;
import com.qjdchina.sku.biz.CategoryBrandManager;
import com.qjdchina.sku.biz.RoleBrandManager;
import com.qjdchina.sku.biz.RoleManager;
import com.qjdchina.sku.biz.SkuProductManager;
import com.qjdchina.sku.model.CategoryBrand;
import com.qjdchina.sku.model.Role;
import com.qjdchina.sku.model.query.BrandQueryVO;
import com.qjdchina.sku.service.enums.SkuResultCode;

@Controller
@RequestMapping(value = "brand")
public class BrandController {
	@Autowired
	private BrandManager brandManager;
	@Autowired
	private RoleManager roleManager;
	@Autowired
	private RoleBrandManager roleBrandManager;
	@Autowired
	private SkuProductManager skuProductManager;
	@Autowired
	private CategoryBrandManager categoryBrandManager;
	/**
	 * 分页查询品牌列表
	 * shenpingfeng
	 * 2017年2月13日
	 * @param brandQueryVO
	 * @param strPage
	 * @param request
	 * @return
	 */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
	public RestBody<PageModel<BrandVO>> query(BrandQueryVO brandQueryVO,
			@RequestParam(value = "pageNo", required = false) String strPage, HttpServletRequest request) {
	
		PageModel<BrandVO> pageModel = new PageModel<BrandVO>();
		int pageNo = 1;
		if (StringUtils.isNotEmpty(strPage) && StringUtils.isNumeric(strPage)) {
			pageNo = Integer.parseInt(strPage);
		}
		pageModel.setPageNo(pageNo);
		pageModel.setPageSize(15);
		
		Result<PageModel<BrandVO>> result = brandManager.queryPagedBrandList(brandQueryVO, pageModel);
		if (!result.isSuccess()) {
			return new RestBody<PageModel<BrandVO>>(result.getCode(), result.getMessage());
		}
		PageModel<BrandVO> pageBrandVO = result.getDataModel();

		return new RestBody<PageModel<BrandVO>>(SkuResultCode.SUCCESS, pageBrandVO);
	}
    /**
     * 创建品牌
     * shenpingfeng
     * 2017年2月14日
     * @param brandVO
     * @param request
     * @return
     */
    @RequestMapping(value = "create",method = RequestMethod.POST)
    @ResponseBody
	public RestBody<String> create(BrandVO brandVO, HttpServletRequest request) {
		Result<String> result = brandManager.save(brandVO);
		if (!result.isSuccess()) {
			return new RestBody<String>(result.getCode(), result.getMessage());
		}
		String label = result.getDataModel();
		Result<BrandVO> bResult = brandManager.getBrandByLabel(label);
		if(!bResult.isSuccess()){
			return new RestBody<>(bResult.getCode(),bResult.getMessage());
		}
		BrandVO brand = bResult.getDataModel();
		//创建角色品牌关联关系
		//获取所有角色  角色绑定品类
    	Result<List<Role>> resultRole = roleManager.getAllRole();
    	if(resultRole.isSuccess()){
    		List<Role> roles = resultRole.getDataModel();
    		for(Role role:roles){
    			RoleBrandVO roleBrandVO = new RoleBrandVO();
    			roleBrandVO.setSkuRoleId(role.getId());
    			roleBrandVO.setSkuBrandId(brand.getId());
    			Result<String> resultCreate = roleBrandManager.create(roleBrandVO);
    			if(!resultCreate.isSuccess()){
    				return new RestBody<>(resultCreate.getCode(),resultCreate.getMessage());
    			}
    		}
    	}
		return new RestBody<String>(SkuResultCode.SUCCESS, result.getDataModel());
	}
    /**
     * 更新
     * shenpingfeng
     * 2017年2月13日
     * @param brandVO
     * @param request
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
	public RestBody<String> update(BrandVO brandVO, HttpServletRequest request) {
    	//判断品牌跟部品之间是否有关联
    	Result<List<SkuProductVO>> sResult = skuProductManager.getProductBySkuBrandId(brandVO.getId());
    	if(sResult.isSuccess()){
    		return new RestBody<String>(SkuResultCode.BRAND_PRODUCT_RELATION);
    	}
    	//更新
		Result<String> result = brandManager.update(brandVO);
		if (!result.isSuccess()) {
			return new RestBody<String>(result.getCode(), result.getMessage());
		}
		return new RestBody<String>(SkuResultCode.SUCCESS, result.getDataModel());
	}
    
    /**
     * 获取所有品牌
     * shenpingfeng
     * 2017年2月15日
     * @param request
     * @return
     */
    @RequestMapping(value = "getAllBrand",method = RequestMethod.GET)
    @ResponseBody
	public RestBody<List<BrandVO>> getAllBrand(HttpServletRequest request) {
		Result<List<BrandVO>> result = brandManager.getAllBrand();
		if (!result.isSuccess()) {
			return new RestBody<List<BrandVO>>(result.getCode(), result.getMessage());
		}
		List<BrandVO> brandVOs = result.getDataModel();
		return new RestBody<List<BrandVO>>(SkuResultCode.SUCCESS, brandVOs);
	}
    /**
     * 更新品类品牌关联关系
     * shenpingfeng
     * 2017年4月6日
     * @param categoryBrandVO
     * @param request
     * @return
     */
    @RequestMapping(value = "updateCategoryBrand",method = RequestMethod.POST)
    @ResponseBody
	public RestBody<String> updateCategoryBrand(CategoryBrandVO categoryBrandVO, @RequestParam(value =
			"categoryBrandAssoVOs") String categoryBrandAssoVOs, @RequestParam(value =
			"categoryBrandRegionVOs") String categoryBrandRegionVOs, HttpServletRequest request) {
		if (StringUtils.isNotBlank(categoryBrandAssoVOs)) {
			List<CategoryBrandAssoVO> categoryBrandAssoVOList =
					JSON.parseArray(categoryBrandAssoVOs, CategoryBrandAssoVO.class);
			categoryBrandVO.setCategoryBrandAssoVOList(categoryBrandAssoVOList);
		}
		if (StringUtils.isNotBlank(categoryBrandRegionVOs)) {
			List<CategoryBrandRegionVO> categoryBrandRegionVOList =
					JSON.parseArray(categoryBrandRegionVOs, CategoryBrandRegionVO.class);
			categoryBrandVO.setCategoryBrandRegionVOList(categoryBrandRegionVOList);
		}
		Result<String> result = categoryBrandManager.update(categoryBrandVO);
		if (!result.isSuccess()) {
			return new RestBody<>(result.getCode(), result.getMessage());
		}
		return new RestBody<String>(SkuResultCode.SUCCESS);
	}
}
